package com.aruba.screenlibrary;

import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * view设置长宽等属性的逻辑处理
 * 需使用px
 */
public class ViewCalculateUtil {
    /**
     * 设置layoutparams
     *
     * @param view
     * @param width
     * @param height
     * @param topMargin
     * @param bottomMargin
     * @param leftMargin
     * @param rightMargin
     */
    public static void setLayoutParams(View view, int width, int height, int topMargin, int bottomMargin, int leftMargin, int rightMargin) {
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();

        if (layoutParams != null) {

            if (width != LinearLayout.LayoutParams.MATCH_PARENT &&
                    width != LinearLayout.LayoutParams.WRAP_CONTENT) {
                layoutParams.width = (int) UIUtils.getInstance().getWidth(width);
            } else {
                layoutParams.width = width;
            }

            if (height != LinearLayout.LayoutParams.MATCH_PARENT && height != LinearLayout.LayoutParams.WRAP_CONTENT) {
                layoutParams.height = (int) UIUtils.getInstance().getHeight(height);
            } else {
                layoutParams.height = height;
            }

            layoutParams.topMargin = (int) UIUtils.getInstance().getHeight(topMargin);
            layoutParams.bottomMargin = (int) UIUtils.getInstance().getHeight(bottomMargin);
            layoutParams.leftMargin = (int) UIUtils.getInstance().getWidth(leftMargin);
            layoutParams.rightMargin = (int) UIUtils.getInstance().getWidth(rightMargin);
            view.setLayoutParams(layoutParams);
        }
    }

    /**
     * 设置字号
     *
     * @param view
     * @param size
     */
    public static void setTextSize(TextView view, int size) {
        view.setTextSize(TypedValue.COMPLEX_UNIT_PX, UIUtils.getInstance().getHeight(size));
    }

    /**
     * 设置view的内边距
     *
     * @param view
     * @param topPadding
     * @param bottomPadding
     * @param leftpadding
     * @param rightPadding
     */
    public static void setViewPadding(View view, int topPadding, int bottomPadding, int leftpadding, int rightPadding) {
        view.setPadding((int) UIUtils.getInstance().getWidth(leftpadding),
                (int) UIUtils.getInstance().getHeight(topPadding),
                (int) UIUtils.getInstance().getWidth(rightPadding),
                (int) UIUtils.getInstance().getHeight(bottomPadding));
    }
}
